<!--
 * @Description:全选反选
 * @Author: Sun
 * @Date: 2019-07-01 20:05:50
 * @LastEditors: Sun
 * @LastEditTime: 2019-07-01 22:55:46
 -->
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>全选反选</title>
  <script>
    window.onload = function () {
      var obtn1 = document.getElementById("btn1")
      var obtn2 = document.getElementById("btn2")
      var obtn3 = document.getElementById("btn3")
      var odiv = document.getElementById("div1")
      var ach = odiv.getElementsByTagName("input")

      obtn1.onclick = function () {
        for (let index = 0; index < ach.length; index++) {
          const element = ach[index];
          element.checked = true
        }
      }

      obtn2.onclick = function () {
        for (let index = 0; index < ach.length; index++) {
          const element = ach[index];
          element.checked = false
        }
      }

       obtn3.onclick = function () {
        for (let index = 0; index < ach.length; index++) {
          const element = ach[index];
          if(element.checked){
            element.checked=false
          }else{
            element.checked=true
          }
        }
      }


    }
  </script>
</head>

<body>
  <input type="button" value="全选" id="btn1">
  <input type="button" value="不选" id="btn2">
  <input type="button" value="反选" id="btn3">
  <div id="div1">
    <input type="checkbox"><br>
    <input type="checkbox"><br>
    <input type="checkbox"><br>
    <input type="checkbox"><br>
    <input type="checkbox"><br>
    <input type="checkbox"><br>
    <input type="checkbox"><br>
    <input type="checkbox"><br>
    <input type="checkbox"><br>
    <input type="checkbox"><br>
  </div>
  <style>

  </style>


</body>

</html>